<template>
    <n-spin :show="loading">
        <n-space vertical :size="12">
            <n-space justify="space-between">
                <n-space>
                    <!-- <n-tag size="large">累计收益：{{ summ.now }}</n-tag> -->
                </n-space>
                <n-space>
                    <n-form inline :model="param" :show-label="false" :show-feedback="false" ref="formRef">
                        <n-form-item>
                            <n-tooltip trigger="hover">
                                <template #trigger>
                                    <n-date-picker v-model:value="dates" style="width: 250px" type="daterange" :update-value-on-close="true" />
                                </template>
                                入账时间段
                            </n-tooltip>
                        </n-form-item>
                    </n-form>

                    <n-button @click="doQuery">
                        <template #icon>
                            <n-icon :component="YoutubeSearchedForRound" />
                        </template>
                        查询
                    </n-button>
                </n-space>
            </n-space>
            <n-data-table striped size="large" :max-height="height" :min-height="items.length > 0 ? height : 0" :row-key="(row:any) => row.id" :columns="columns" :data="items" />
            <n-space justify="end">
                <n-pagination show-size-picker :page="param.pager.page" :page-size="param.pager.size" :page-count="pager.all" :page-sizes="[20, 50, 100]" @update:page="onChangePage" @update:page-size="onChangeSize">
                    <template #prefix="{}">当前 {{ pager.now }} / {{ pager.all }} 页，共 {{ pager.row }} 条数据</template>
                </n-pagination>
            </n-space>
        </n-space>
    </n-spin>
</template>

<script lang="ts" setup>
import { computed, ref, h, onActivated, watch } from 'vue'
import { YoutubeSearchedForRound } from '@vicons/material'
import { useStore } from '@/store'
import { NButton, NSpace, NTag, NText, NTooltip } from 'naive-ui'
import { Http } from '@/libs'

import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn' // 导入本地化语言
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.locale('zh-cn')
dayjs.extend(customParseFormat)

const store = useStore()
const height = computed(() => store.state.browser.innerHeight - 310)

const loading = computed(() => store.state.loading.flag)
const columns = [
    {
        title: '入账时间',
        key: 'time',
        align: 'left',
        width: 180,
        ellipsis: {
            tooltip: true,
        },
    },
    {
        title: '入账金额',
        key: 'value',
        align: 'right',
        width: 120,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []

            if (row.flag) {
                comps[comps.length] = h(NText, { type: 'info' }, { default: () => (row.value / 100).toFixed(2) })
            } else {
                comps[comps.length] = h(NText, { type: 'error' }, { default: () => (row.value / 100).toFixed(2) })
            }
            return h(NSpace, {}, { default: () => comps })
        },
    },
    {
        title: '累计收益',
        key: 'value',
        align: 'right',
        width: 120,
        ellipsis: {
            tooltip: true,
        },
        render(row: any) {
            let comps: any[] = []

            if (parseFloat(row.after) >= 0) {
                comps[comps.length] = h(NText, { type: 'info' }, { default: () => (row.after / 100).toFixed(2) })
            } else {
                comps[comps.length] = h(NText, { type: 'error' }, { default: () => (row.after / 100).toFixed(2) })
            }
            return h(NSpace, {}, { default: () => comps })
        },
    },
    {
        title: '入账说明',
        key: 'memo',
        align: 'left',
        ellipsis: {
            tooltip: true,
        },
    },
]

const onChangeSize = (size: number) => {
    param.value.pager.page = 1
    param.value.pager.size = size
    execLoad()
}

const onChangePage = (page: number) => {
    param.value.pager.page = page
    execLoad()
}

const dates = ref<number[]>([dayjs().subtract(1, 'month').valueOf(), dayjs().valueOf()])
watch([dates, () => dates.value], (nv, _ov) => {
    let v = nv[0] as number[]
    param.value.query.begin = dayjs(v[0]).format('YYYY-MM-DD')
    param.value.query.close = dayjs(v[1]).format('YYYY-MM-DD')
})

const param = ref({
    query: {
        uid: '1',
        wallet: undefined,
        begin: dayjs().subtract(1, 'month').format('YYYY-MM-DD'),
        close: dayjs().format('YYYY-MM-DD'),
    },
    pager: {
        page: 1,
        size: 20,
        time: new Date().getTime(),
    },
})

const pager = ref({
    now: 1,
    row: 0,
    all: 0,
    fms: 0,
})
const items = ref([])

const summ = ref<any>({})

const doQuery = () => {
    param.value.pager.page = 1
    execLoad()
}

const execLoad = () => {
    Http.post('/ops/fin/info/list', param.value).then(resp => {
        if (resp.stat.err > 0) {
            return
        }
        if (resp.data.pager.now == 1) {
            summ.value = resp.data.summ
            param.value.pager.time = resp.data.pager.fms
        }
        pager.value = resp.data.pager
        items.value = resp.data.items
    })
}
onActivated(() => {
    execLoad()
})
</script>
